<?php
/******************************
 *
 * 时间；2017年11月15日
 * 功能：订单接口
 * 作者：Mr Peng
 *
 *****************************/ 
namespace Api\Controller;
use Think\Controller;
header('Content-Type:text/json;charset=utf-8');
header("Access-Control-Allow-Origin: *");
class OrderController extends CommonController {

    //约课币支付
    public function payment(){
        if(IS_POST){
            $token=I('post.token');
            $order_id=I('post.order_id');
            $paypass=I('post.paypass');
            if($token==''||$order_id==''||$paypass==''){
                $this->templateApi('','204','参数错误');exit;
            }else{
                $user=M('user')->where(array('token'=>$token))->find();
                if($user){
                    $order=M('order');
                    $orderrel=$order->where(array('order_id'=>$order_id))->find();
                    if($orderrel){

                    }else{
                        $this->templateApi('','202','订单信息错误');exit;
                    }
                    $udrel=M('userdata')->where(array('uid'=>$user['user_id']))->find();
                    if($udrel['paypass']==md5(md5($paypass))){
                        if($udrel['money']<$orderrel['money']){
                            $this->templateApi('','202','约课币不足请充值');exit;
                        }
                        $good=M('goods');
                        $goodrel=$good->where(array('goods_id'=>$orderrel['pid']))->find();
                        $class=M('class')->where(array('class_id'=>$goodrel['class_id']))->find();
                        //如果是团购，扣掉学生的钱，并修改团购记录表的支付状态
                        if($orderrel['buytype']==2){
                            $jian=M('userdata')->where(array('uid'=>$user['user_id']))->setDec('money',$orderrel['money']);
                            //修改订单支付状态
                            $mmp['status']=1;
                            $order->where(array('order_id'=>$orderrel['order_id']))->save($mmp);
                            //修改团购记录表支付状态
                            $mvp['paystatus']=1;
                            M('groupbuy')->where(array('uid'=>$user['user_id'],'pid'=>$orderrel['pid']))->save($mvp);
                            //修改课程报名人数
                            $mvpg['reg_status']=1;
                            $mvpg['number']=$goodrel['number']+1;
                            $good->where(array('goods_id'=>$goodrel['goods_id']))->save($mvpg);
                            $this->templateApi('','200','ok');exit;
                        }
                        //开启事务
                        M('userdata')->startTrans();
                        M('class')->startTrans();
                        if($goodrel['type']==1||$goodrel['type']==5){
                            if($goodrel['reg_status']==1){
                                $this->templateApi('','202','您看中的课程已经被购买');exit;
                            }
                            $jia=M('class')->where(array('class_id'=>$class['class_id']))->setInc('profit',$orderrel['money']);
                            $jian=M('userdata')->where(array('uid'=>$user['user_id']))->setDec('money',$orderrel['money']);
                            if($jia && $jian){
                                M('userdata')->commit();
                                M('class')->commit();
                                //如果是一对一直播或者预约课，修改报名状态和报名人id并下架
                                $mvp['reg_status']=1;
                                $mvp['status']=1;
                                $mvp['buy_uid']=$user['user_id'];
                                $mvp['number']=$goodrel['number']+1;
                                $good->where(array('goods_id'=>$goodrel['goods_id']))->save($mvp);
                                //查询该老师是否还有预约课
                                $isAppointment['order_status']=0;
                                $tyue=$good->where(array('class_id'=>$goodrel['class_id'],'type'=>'5','status'=>0))->find();
                                if($tyue){

                                }else{
                                    M('class')->where(array('class_id'=>$goodrel['class_id']))->save($isAppointment);
                                }
                                //添加学生交易记录
                                $vm['uid']=$user['user_id'];
                                $vm['type']=2;
                                $vm['time']=time();
                                $vm['cid']=$goodrel['goods_id'];
                                $vm['pay']=$orderrel['money'];
                                if($goodrel['type']==5){
                                    $vm['source']=date('Y-m-d H:i:s',$goodrel['starttime']).'-'.date('H:i:s',$goodrel['endtime']).'预约课';
                                }else{
                                    $vm['source']=$goodrel['name'];
                                }

                                $vm['ptype']=2;
                                $vm['ctype']=$goodrel['type'];
                                M('payrecord')->add($vm);
                                //添加老师交易记录
                                $tvm['uid']=$class['uid'];
                                $tvm['type']=1;
                                $tvm['time']=time();
                                $tvm['cid']=$goodrel['goods_id'];
                                $tvm['income']=$orderrel['money'];
                                if($goodrel['type']==5){
                                    $tvm['source']=date('Y-m-d H:i:s',$goodrel['starttime']).'-'.date('H:i:s',$goodrel['endtime']).'预约课';
                                }else{
                                    $tvm['source']=$goodrel['name'];
                                }
                                $tvm['ptype']=2;
                                $tvm['ctype']=$goodrel['type'];
                                M('payrecord')->add($tvm);
                                //修改订单支付状态
                                $mmp['status']=1;
                                $order->where(array('order_id'=>$orderrel['order_id']))->save($mmp);
                                //删除其他待支付订单
                                $order->where(array('pid'=>$goodrel['goods_id'],'status'=>0))->delete();
                                $this->templateApi('','200','ok');
                            }else{
                                M('userdata')->rollback();
                                M('class')->rollback();
                                $this->templateApi('','202','err');
                            }

                        }else{
                            $jia=M('class')->where(array('class_id'=>$class['class_id']))->setInc('profit',$orderrel['money']);
                            $jian=M('userdata')->where(array('uid'=>$user['user_id']))->setDec('money',$orderrel['money']);
                            if($jia && $jian){
                                M('userdata')->commit();
                                M('class')->commit();
                                //修改课程报名人数，和报名状态
                                $mvp['reg_status']=1;
                                $mvp['number']=$goodrel['number']+1;
                                $good->where(array('goods_id'=>$goodrel['goods_id']))->save($mvp);
                                //添加学生交易记录
                                $vm['uid']=$user['user_id'];
                                $vm['type']=2;
                                $vm['time']=time();
                                $vm['cid']=$goodrel['goods_id'];
                                $vm['pay']=$orderrel['money'];
                                $vm['source']=$goodrel['name'];
                                $vm['ptype']=2;
                                $vm['ctype']=$goodrel['type'];
                                M('payrecord')->add($vm);
                                //添加老师交易记录
                                $tvm['uid']=$class['uid'];
                                $tvm['type']=1;
                                $tvm['time']=time();
                                $tvm['cid']=$goodrel['goods_id'];
                                $tvm['income']=$orderrel['money'];
                                $tvm['source']=$goodrel['name'];
                                $tvm['ptype']=2;
                                $tvm['ctype']=$goodrel['type'];
                                M('payrecord')->add($tvm);
                                //修改订单支付状态
                                $mmp['status']=1;
                                $order->where(array('order_id'=>$orderrel['order_id']))->save($mmp);
                                $this->templateApi('','200','ok');
                            }else{
                                M('userdata')->rollback();
                                M('class')->rollback();
                                $this->templateApi('','202','err');
                            }

                        }


                    }else{
                        $this->templateApi('','202','支付密码不正确');
                    }
                }else{
                    $this->templateApi('','300','未登录');
                }
            }
        }else{
            $this->templateApi('','203','请求类型错误');
        }
    }
// +----------------------------------------------------------------------
// | 功能       | 订单列表
// +----------------------------------------------------------------------
// | 请求类型   | post
// +----------------------------------------------------------------------
// | 参数       | token,screen,page
// +----------------------------------------------------------------------
    public function order(){

        if(IS_POST){
            $token=I('post.token');
            $screen=I('post.screen');
            $page=I('post.page');
            if($token==''||$page==''){
                $this->templateApi('','204','参数错误');
            }else{
                $user=M('user')->where(array('token'=>$token))->find();
                if($user){
                    //删除超过6小时的订单
                    $delwhere['uid']=$user['user_id'];
                    $delwhere['time']=array('elt',time()-21600);
                    $delwhere['status']=0;
                    M('order')->where($delwhere)->delete();
                    //修改团购失败的订单为取消
                    $groupwhere['uid']=$user['user_id'];
                    $groupwhere['buytype']=2;
                    $groupwhere['status']=1;
                    $grouprel=M('order')->where($groupwhere)->select();
                    foreach($grouprel as $k=>$v){
                        $groupgood=M('goods')->where(array('goods_id'=>$v['pid']))->find();
                        if($groupgood['group_status']==2){
                            $gmp['status']=2;
                            M('order')->where(array('order_id'=>$v['order_id']))->save($gmp);
                        }
                    }
                    //修改团购成功的订单为完成
                    $groupwhere1['uid']=$user['user_id'];
                    $groupwhere1['buytype']=2;
                    $groupwhere1['status']=1;
                    $grouprel1=M('order')->where($groupwhere1)->select();
                    foreach($grouprel1 as $k=>$v){
                        $groupgood=M('goods')->where(array('goods_id'=>$v['pid']))->find();
                        if($groupgood['group_status']==1){
                            $gmp['status']=1;
                            M('order')->where(array('order_id'=>$v['order_id']))->save($gmp);
                        }
                    }

                    $where['uid']=$user['user_id'];
                    if($screen=='0'){
                        $where['status']=0;
                    }else if($screen=='1'){
                        $where['status']=1;
                    }else if($screen=='2'){
                        $where['status']=2;
                    }else if($screen=='3'){
                        $where['status']=1;
                        $where['buytype']=2;
                    }else{

                    }

                    $count=M('order')->where($where)->count();
                    $data=$this->havePage('order',$where,'time desc',$page,'20','');
                    $pie['now_page']=$data['now_page'];
                    $pie['total_page']=$data['total_page'];
                    unset($data['now_page']);
                    unset($data['total_page']);
                    foreach($data as $k=>$v){
                        $good=M('goods')->where(array('goods_id'=>$v['pid']))->find();
                        if($screen=='3'){
                            if($good['group_status']==1 || $good['group_status']==2){
                                unset($k);
                                continue;
                            }
                        }
                        if($screen=='1'){
                            if($v['buytype']==2){
                                if($good['group_status']!=1){
                                    unset($k);
                                    continue;
                                }
                            }
                        }
                        if($screen=='2'){
                            if($v['buytype']==2){
                                if($good['group_status']!=2){
                                    unset($k);
                                    continue;
                                }
                            }
                        }
                        $date[$k]['oid']=(string)$v['order_id'];
                        $date[$k]['orderNum']=(string)$v['num'];
                        $date[$k]['time']=(string)date('Y-m-d',$v['time']);
                        $date[$k]['status']=$v['status'];       //状态   0:待支付  1：OK  2：err

                        $class=M('class')->where(array('class_id'=>$good['class_id']))->find();
                        if($class['type']==3){
                            $date[$k]['isOrganization']='1';
                        }else{
                            $date[$k]['isOrganization']='l';
                        }
                        $tuser=M('user')->where(array('user_id'=>$class['uid']))->find();
                        $udrel=M('userdata')->where(array('uid'=>$class['uid']))->find();
                        $date[$k]['teacherName']=$tuser['name'];
                        $date[$k]['teacherImgURL']=URL.$udrel['image'];
                        $date[$k]['startTime']=date('Y-m-d H:i:s',$good['starttime']);
                        //分类start
                        $ca=M('category');
                        $cate=$ca->where(array('cate_id'=>$good['cate_id']))->find();
                        $cate1=$ca->where(array('cate_id'=>$cate['pid']))->find();
                        $cate2=$ca->where(array('cate_id'=>$cate1['pid']))->find();
                        $date[$k]['className']=(string)$cate2['name'].'|'.$cate1['name'];
                        //分类end
                        $date[$k]['endTime']=date('H:i:s',$good['endtime']);
                        $date[$k]['img']=(string)URL.$good['img'];
                        $date[$k]['name']=$good['name'];
                        $date[$k]['money']=(string)$v['money'];
                        $date[$k]['pid']=(string)$good['goods_id'];
                        $date[$k]['pType']=(string)$good['type'];
                        if($good['group_time']){
                            $date[$k]['group_time']=date('Y-m-d H:i:s',$good['group_time']);
                        }else{
                            $date[$k]['group_time']='';
                        }
                        if($v['buytype']==1){
                            $date[$k]['group_status']='0';      //是否团购   1：是
                        }else{
                            $date[$k]['group_status']='1';
                        }
                        if($v['buytype']==2){
                            $wh['pid']=$good['goods_id'];
                            $wh['paystatus']=1;
                            $grcount=M('groupbuy')->where($wh)->count();
                            if($grcount>$good['group_num']){
                                $date[$k]['num']='0';
                            }else{
                                $date[$k]['num']=(string)($good['group_num']-$grcount);
                            }
                        }else{
                            $date[$k]['num']='';
                        }
                        $report=M('report')->where(array('uid'=>$user['user_id'],'order_id'=>$good['goods_id']))->find();
                        if($report){
                            $date[$k]['report_status']='1';
                        }else{
                            $date[$k]['report_status']='0';
                        }
                    }

                    if(empty($date)){
                        $arr=array();
                    }else{
                        $arr=array_values($date);
                        $newarr=array();
                        for($i=0;$i<count($arr);$i++){
                            array_push($newarr,$arr[$i]);
                        }
                        $rel['data']=$newarr;
                    }

                    $rel['now_page']=$pie['now_page'];
                    $rel['total_page']=$pie['total_page'];
                    $rel['count']=$count;
                    $this->templateApi($rel,'200','ok');
                }else{
                    $this->templateApi('','300','未登录');
                }
            }
        }else{
            $this->templateApi('','203','请求类型错误');
        }
    }

    //订单详情
    public function orderdetails(){
        if(IS_POST){
            $token=I('post.token');
            $orderid=I('post.order_id');
            if($token==''||$orderid==''){
                $this->templateApi('','204','参数错误');exit;
            }
            $user=M('user')->where(array('token'=>$token))->find();
            if($user){
                $order=M('order')->where(array('order_id'=>$orderid))->find();
                $pid=$order['pid'];
                $good=M('goods')->where(array('goods_id'=>$order['pid']))->find();
                $data['oid']=(string)$order['order_id'];
                $data['img']=(string)URL.$good['img'];
                $data['name']=(string)$good['name'];
                $data['introduce']=$good['introduce'];
                $data['money']=(string)$order['money'];
                $data['pid']=(string)$good['goods_id'];
                $data['pType']=(string)$good['type'];
                $data['status']=(string)$order['status'];
                $data['ordernum']=(string)$order['num'];
                $data['time']=(string)date('Y-m-d H:i:s',$order['time']);
                if($order['paytype']){
                    $data['paytype']=(string)$order['paytype']; //支付方式  0：约课币  1：支付宝  2：微信
                }else{
                    $data['paytype']='';
                }

                $data['grouptime']=(string)$good['group_time']; //开团时间
                if($order['buytype']==2){
                    $groupbuy=M('groupbuy')->where(array('pid'=>$good['goods_id'],'paystatus'=>1))->count();
                    if($groupbuy<$good['group_num']){
                        $data['num']=(string)($good['group_num']-$groupbuy);
                    }else{
                        $data['num']='0';
                    }
                }else{
                    $data['num']='';
                }
                $report=M('report')->where(array('uid'=>$user['user_id'],'order_id'=>$pid))->find();
                if($report){
                    $data['report_status']='1';
                }else{
                    $data['report_status']='0';
                }
                $class=M('class')->where(array('class_id'=>$good['class_id']))->find();
                if($class['type']==3){
                    $data['isOrganization']='1';
                }else{
                    $data['isOrganization']='l';
                }
                $tuser=M('user')->where(array('user_id'=>$class['uid']))->find();
                $udrel=M('userdata')->where(array('uid'=>$class['uid']))->find();
                $data['teacherName']=$tuser['name'];
                $data['teacherImgURL']=URL.$udrel['image'];
                $data['startTime']=date('Y-m-d H:i:s',$good['starttime']);
                $data['endTime']=date('H:i:s',$good['endtime']);
                //分类start
                $ca=M('category');
                $cate=$ca->where(array('cate_id'=>$good['cate_id']))->find();
                $cate1=$ca->where(array('cate_id'=>$cate['pid']))->find();
                $cate2=$ca->where(array('cate_id'=>$cate1['pid']))->find();
                $data['className']=(string)$cate2['name'].'|'.$cate1['name'];
                //分类end
                $this->templateApi($data,'200','ok');
            }else{
                $this->templateApi('','300','未登录');
            }
        }else{
            $this->templateApi('','203','请求类型错误');
        }
    }

// +----------------------------------------------------------------------
// | 功能       | 取消订单
// +----------------------------------------------------------------------
// | 请求类型   | post
// +----------------------------------------------------------------------
// | 参数       | token、order_id
// +----------------------------------------------------------------------
    public function cancelorder(){
        if(IS_POST){
            $token=I('post.token');
            $order_id=I('post.order_id');
            if($token==''||$order_id==''){
                $data=(object)null;
                echo json_encode($this->apiTemplate($data,'204','参数错误'));
            }else{
                $userrel=M('user')->where(array('token'=>$token))->find();
                if($userrel){
                    $mvp['status']='2';
                    $rel=M('order')->where(array('order_id'=>$order_id))->save($mvp);
                    if($rel!==false){
                        $data=(object)null;
                        echo json_encode($this->apiTemplate($data,'200','ok'));
                    }else{
                        $data=(object)null;
                        echo json_encode($this->apiTemplate($data,'202','失败'));
                    }
                }else{  
                    $data=(object)null;
                    echo json_encode($this->apiTemplate($data,'300','未登录'));
                }
            }
        }else{
            $data=(object)null;
            echo json_encode($this->apiTemplate($data,'203','请求类型错误'));
        }
    }
// +----------------------------------------------------------------------
// | 功能       | 删除订单
// +----------------------------------------------------------------------
// | 请求类型   | post
// +----------------------------------------------------------------------
// | 参数       | token、order_id
// +----------------------------------------------------------------------
    public function delorder(){
        if(IS_POST){
            $token=I('post.token');
            $order_id=I('post.order_id');
            if($token==''||$order_id==''){
                $data=(object)null;
                echo json_encode($this->apiTemplate($data,'204','参数错误'));
            }else{
                $userrel=M('user')->where(array('token'=>$token))->find();
                if($userrel){
                    $rel=M('order')->where(array('order_id'=>$order_id))->delete();
                    if($rel){
                        $data=(object)null;
                        echo json_encode($this->apiTemplate($data,'200','ok'));
                    }else{
                        $data=(object)null;
                        echo json_encode($this->apiTemplate($data,'202','失败'));
                    }
                }else{  
                    $data=(object)null;
                    echo json_encode($this->apiTemplate($data,'300','未登录'));
                }
            }
        }else{
            $data=(object)null;
            echo json_encode($this->apiTemplate($data,'203','请求类型错误'));
        }
    }

    //订单确认接口
    public function index(){
        $data=(object)null;
        if(IS_POST){
            $pid=I('post.pId');         //课程id
            $token=I('post.token');     //用户token
            $type=I('post.pBuyType');   //价格类型
            $buytype=I('post.buytype'); //什么方式购买  原价0   1：优惠
            if($pid==''||$token==''||$type==''||$buytype==''){
                $this->templateApi($data,'204','参数错误');exit;
            }
            $userrel=M('user')->where(array('token'=>$token))->find();
            if(!$userrel){
                $this->templateApi($data,'300','token过期');exit;
            }
            $goods=M('goods');
            $goodrel=$goods->where(array('goods_id'=>$pid))->find();
            if($buytype==1 && $goodrel['price_status']==2 && $goodrel['group_time']<time()){
                $this->templateApi('','202','团购时间已过');exit;
            }
            if($goodrel){
                $date['orderID']=(string)time().$this->getRandomString(5);
                if($goodrel['img']){
                    $date['imgURL']=(string)URL.$goodrel['img'];
                }else{
                    $date['imgURL']='';
                }
                $date['pType']=(string)$goodrel['type'];
                $date['pName']=(string)$goodrel['name'];
                $date['pInfo']=(string)$goodrel['introduce'];
                if($type=='0'){
                    $date['pPrice']=(string)$goodrel['money'];
                }else{
                    if($buytype=='0'){
                        $date['pPrice']=(string)$goodrel['money'];
                    }else{
                        $date['pPrice']=(string)$goodrel['discount_money'];
                    }
                }
                if($goodrel['price_status']==2){
                    if($buytype=='0'){
                        $date['payStyle']='1';  //1
                    }else{
                        $date['payStyle']='0';  //为了测试  都改成只能约课币支付   0:只能约课币   1：都可以
                    }
                }else{
                    $date['payStyle']='1';  //1
                }
                $class=M('class')->where(array('class_id'=>$goodrel['class_id']))->find();
                if($class['type']==3){
                    $date['isOrganization']='1';
                }else{
                    $date['isOrganization']='0';
                }
                $tudrel=M('userdata')->where(array('uid'=>$class['uid']))->find();
                $tuser=M('user')->where(array('user_id'=>$tudrel['uid']))->find();
                $date['teacherName']=$tuser['name'];        //老师名
                $date['teacherImgURL']=URL.$tudrel['image'];   //老师头像
                $cate=M('category');
                $fcate=$cate->where(array('cate_id'=>$goodrel['cate_id']))->find();
                $ffcate=$cate->where(array('cate_id'=>$fcate['pid']))->find();
                $date['className']=(string)$ffcate['name'].'|'.(string)$fcate['name'];  //分类名
                $date['startTime']=(string)date('Y年m月d日 H:i:s',$goodrel['starttime']);
                $date['endTime']=(string)date('H:i:s',$goodrel['endtime']);
                $udrel=M('userdata')->where(array('uid'=>$userrel['user_id']))->find();
                $date['ykenum']=$udrel['money'];
                if($udrel['money']>$date['pPrice'] || $udrel['money']==$date['pPrice']){
                    $date['ykbCanPay']='1';
                }else{
                    $date['ykbCanPay']='0';
                }
                $this->templateApi($date,'200','ok');
            }else{
                $this->templateApi($data,'202','商品id错误');
            }
        }else{
            $this->templateApi($data,'203','请求类型错误');
        }
    }

    //待支付订单跳转订单确认接口
    public function indextwo(){
        if(IS_POST){
            $orderid=I('post.orderid');
            if($orderid==''){
                $this->templateApi($data,'204','参数错误');exit;
            }
            $order=M('order')->where(array('order_id'=>$orderid))->find();
            if($order){
                $goodrel=M('goods')->where(array('goods_id'=>$order['pid']))->find();
                if($order['buytype']==2 && $goodrel['price_status']==2 && $goodrel['group_time']<time()){
                    $this->templateApi('','202','团购时间已过');exit;
                }
                if($goodrel['img']){
                    $date['imgURL']=(string)URL.$goodrel['img'];
                }else{
                    $date['imgURL']='';
                }
                $date['pType']=(string)$goodrel['type'];
                $date['pName']=(string)$goodrel['name'];
                $date['pInfo']=(string)$goodrel['introduce'];
                $date['pPrice']=(string)$order['money'];
                if($goodrel['price_status']==2){
                    if($buytype=='0'){
                        $date['payStyle']='1';  //1
                    }else{
                        $date['payStyle']='0';  //为了测试  都改成只能约课币支付
                    }
                }else{
                    $date['payStyle']='1';  //1
                }
                $class=M('class')->where(array('class_id'=>$goodrel['class_id']))->find();
                if($class['type']==3){
                    $date['isOrganization']='1';
                }else{
                    $date['isOrganization']='0';
                }
                $tudrel=M('userdata')->where(array('uid'=>$class['uid']))->find();
                $tuser=M('user')->where(array('user_id'=>$tudrel['uid']))->find();
                $date['teacherName']=$tuser['name'];        //老师名
                $date['teacherImgURL']=URL.$tudrel['image'];   //老师头像
                $cate=M('category');
                $fcate=$cate->where(array('cate_id'=>$goodrel['cate_id']))->find();
                $ffcate=$cate->where(array('cate_id'=>$fcate['pid']))->find();
                $date['className']=(string)$ffcate['name'].'|'.(string)$fcate['name'];  //分类名
                $date['startTime']=(string)date('Y年m月d日 H:i:s',$goodrel['starttime']);
                $date['endTime']=(string)date('H:i:s',$goodrel['endtime']);
                $udrel=M('userdata')->where(array('uid'=>$order['uid']))->find();
                $date['ykenum']=$udrel['money'];
                if($udrel['money']>$date['pPrice'] || $udrel['money']==$date['pPrice']){
                    $date['ykbCanPay']='1';
                }else{
                    $date['ykbCanPay']='0';
                }
                $this->templateApi($date,'200','ok');
            }else{
                $this->templateApi($data,'202','订单错误');
            }
        }else{
            $this->templateApi($data,'203','请求类型错误');
        }
    }

    //加入课程接口
    public function joinclass(){
        if(IS_POST){
            $token=I('post.token');
            $cid=I('post.cId');
            if($token==''||$cid==''){
                $this->templateApi('','204','参数错误');exit;
            }
            $user=M('user')->where(array('token'=>$token))->find();
            if($user){
                $good=M('goods');
                $goodrel=$good->where(array('goods_id'=>$cid))->find();
                $payrecord=M('payrecord');
                $pay=$payrecord->where(array('uid'=>$user['user_id'],'cid'=>$cid))->find();
                if($pay){
                    $this->templateApi('','200','ok');
                }else{
                    $mvp=array(
                        'uid'   =>  $user['user_id'],
                        'type'  =>  2,
                        'time'  =>  time(),
                        'cid'   =>  $cid,
                        'pay'   =>  0,
                        'source'=>  $goodrel['name'],
                        'ptype' =>  2,
                        'ctype' =>  $goodrel['type'],
                    );
                    $payrecord->add($mvp);
                    $mmp['number']=$goodrel['number']+1;
                    $mmp['reg_status']=1;
                    $good->where(array('goods_id'=>$goodrel['goods_id']))->save($mmp);
                    $this->templateApi('','200','ok');
                }
            }else{
                $this->templateApi('','300','未登录');
            }
        }else{
            $this->templateApi('','203','请求类型错误');
        }
    }

    //提交订单
    public function suborder(){
        if(IS_POST){
            $token=I('post.token');
            $ordernum=I('post.ordernum');
            $money=I('post.money');
            $pid=I('post.pId');
            $type=I('post.pBuyType');       //价格类型
            $buytype=I('post.buytype');     //什么方式购买    0原价， 1优惠
            if($token==''||$ordernum==''||$money==''||$pid==''||$buytype==''||$type==''){
                $this->templateApi('','204','参数错误');exit;
            }
            $user=M('user')->where(array('token'=>$token))->find();
            if($user){
                $good=M('goods')->where(array('goods_id'=>$pid))->find();
                if($good){
                    $payrecord=M('payrecord')->where(array('uid'=>$user['user_id'],'cid'=>$pid))->find();
                    if($payrecord){
                        $this->templateApi('','202','您已经购买过了');exit;
                    }
                    if($good['type']!=2){
                        if($money=='0'){
                            $this->templateApi('','202','价格错误');exit;
                        }
                    }
                    $order=M('order');
                    $orderrel=$order->where(array('uid'=>$user['user_id'],'pid'=>$pid,'status'=>'0'))->find();
                    if($orderrel){
                        $order->where(array('order_id'=>$orderrel['order_id']))->delete();
                    }
                    if($type=='0'){
                       $mvp['money']=$good['money'];
                    }else{
                        if($buytype=='0'){
                            $mvp['money']=$good['money'];
                        }else{
                            $mvp['money']=$good['discount_money'];
                        }
                    }
                    if($buytype=='1'){
                        $groupbuy=M('groupbuy');
                        $grel=$groupbuy->where(array('uid'=>$user['user_id'],'pid'=>$pid,'status'=>'0','paystatus'=>1))->find();
                        if($grel){
                            $this->templateApi('','202','您已参加该团购了');exit;
                        }else{
                            $vm['uid']=$user['user_id'];
                            $vm['pid']=$pid;
                            $vm['money']=$money;
                            $vm['time']=time();
                            $groupbuy->add($vm);
                        }
                    }
                    $mvp['uid']=$user['user_id'];
                    $mvp['pid']=$pid;
                    $mvp['num']=$ordernum;
                    $mvp['time']=time();
                    $mvp['type']=$good['type'];
                    if($good['price_status']==2){
                        if($buytype=='1'){
                            $mvp['buytype']=2;
                        }else{
                            $mvp['buytype']=1;
                        }
                    }else{
                        $mvp['buytype']=1;
                    }

                    $data['orderid']=$order->add($mvp);

                    $this->templateApi($data,'200','ok');
                }else{
                    $this->templateApi('','202','课程信息错误');
                }
            }else{
                $this->templateApi('','300','未登录');
            }
        }else{
            $this->templateApi('','203','请求类型错误');
        }
    }

    //提交会员充值订单
    public function viporder(){
        if(IS_POST){
            $token=I('post.token');
            $type=I('post.type');   //类型  0：月   1：季  2：年
            $money=I('post.money');
            if($token==''||$type==''||$money==''){
                $this->templateApi('','204','参数错误');exit;
            }
            $user=M('user')->where(array('token'=>$token))->find();
            if($user){
                $mvp['uid']=$user['user_id'];
                $mvp['type']=$type;
                $mvp['money']=$money;
                $mvp['time']=time();
                $data['viporderid']=M('vip')->add($mvp);
                $this->templateApi($data,'200','ok');
            }else{
                $this->templateApi('','300','未登录');
            }
        }else{
            $this->templateApi('','203','请求类型错误');
        }
    }

}